<?php


namespace app\api\service\activity_order;

use app\api\model\GrActivity;
use app\api\model\GrActivityOrder;
use app\api\service\Token;
use app\lib\exception\ParameterException;
use think\Db;

class CreateGrOrder extends Token
{
    /**
     * 个人活动上车操作
     * 1. 判断队伍是否已满
     * 2. 是否已报名
     * 3. 创建信息
     * 4. 返回支付number
     */

    // 活动
    protected $grActivity;

    // 用户信息
    protected $user;

    function __construct($id){
        $this->grActivity = GrActivity::where('id',$id)->with('imgs.img')->find();
    }

    // 入口方法
    public function entrance()
    {
        $this->user = self::judgeUser();
        return $this->judgeData();
    }

    // 判断活动信息
    protected function judgeData()
    {
        $time = time();
        $info = Db::table('gr_activity')
            ->where('id',$this->grActivity['id'])
            ->find();
        if($time > $info['end_time']) {
            throw new ParameterException([
                'msg' => [
                    'order' => '活动已经结束'
                ]
            ]);
        }
        if($time > $info['start_time']) {
            throw new ParameterException([
                'msg' => [
                    'order' => '活动已经开始'
                ]
            ]);
        }
        return $this->judgeOrder();
    }

    // 判断订单
    protected function judgeOrder()
    {
        if($this->grActivity['num_status'] > 0){
            $where[] = ['activity_id', 'eq', $this->grActivity['id']];
            if($this->grActivity['num_status'] == 2) {
                if($this->user['gender'] == 3) {
                    throw new ParameterException([
                        'msg' => [
                            'order' => '请点击头像进入更改性别'
                        ]
                    ]);
                }
                $where[] = ['gender', 'eq', $this->user['gender']];
            }
            $count = GrActivityOrder::where($where)->count();
            $count = $count + 1;
            if($this->grActivity['num_status'] == 1) {
                if($count > $this->grActivity['number']) {
                    throw new ParameterException([
                        'msg' => [
                            'order' => '报名人数已满'
                        ]
                    ]);
                }
            } elseif($this->grActivity['num_status'] == 2) {
                if($this->user['gender'] == 1) {
                    if($count > $this->grActivity['man_num']) {
                        throw new ParameterException([
                            'msg' => [
                                'order' => '报名人数已满'
                            ]
                        ]);
                    }
                } elseif($this->user['gender'] == 2) {
                    if($count > $this->grActivity['women_num']) {
                        throw new ParameterException([
                            'msg' => [
                                'order' => '报名人数已满'
                            ]
                        ]);
                    }
                }
            }
        }
        return $this->createData();
    }

    // 创建订单
    protected function createData()
    {
        GrActivityOrder::create([
            'openid' => $this->user['openid'],
            'activity_id' => $this->grActivity['id'],
            'activity_msg' => json_encode($this->grActivity),
            'gender' => $this->user['gender'],
            'pay_time' => time()
        ]);
        return [
            'pay_type' => 1
        ];
    }
}